<!DOCTYPE html>
<html>
<head>
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?d890b1f16fb364253e79c5bb20225c3a";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>


    

    

    
<!-- Baidu Tongji -->
<script>var _hmt = _hmt || []</script>
<script async src="//hm.baidu.com/hm.js?busuanzi_value_site_uv"></script>
<!-- End Baidu Tongji -->




    <meta charset="utf-8">
    <meta name="baidu-site-verification" content="FYMCShbUK8" />
    <meta name="baidu-site-verification" content="ZYRF7OxQRW" />
    <meta name="baidu-site-verification" content="cHSqtjI0PN" />
    <meta name="baidu-site-verification" content="cHSqtjI0PN" />
    <meta name="baidu-site-verification" content="cHSqtjI0PN" />
    
    
    <link rel="canonical" href="https://hhardyy.com/2018/06/10/Vue全家桶/">
    
    
    <title>Vue全家桶 | 小方块 - hhardyy.com | 复杂的坑+归其原理+了解实现规则===解决？解决成功：加油解决成功;</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#958e93">
    
    
    <meta name="keywords" content="vue-cli,vue-router,vuex,axios,刷新404,store分模块,路由,store">
    <meta name="description" content="vue全家桶也就是1.脚手架、2.路由、3.状态管理、4.请求工具~。">
<meta name="keywords" content="vue-cli,vue-router,vuex,axios,刷新404,store分模块,路由,store">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue全家桶">
<meta property="og:url" content="http://yoursite.com/2018/06/10/Vue全家桶/index.html">
<meta property="og:site_name" content="小方块 - hhardyy.com">
<meta property="og:description" content="vue全家桶也就是1.脚手架、2.路由、3.状态管理、4.请求工具~。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://yoursite.com/images/vue-family/vue-cli.png">
<meta property="og:image" content="http://yoursite.com/images/vue-family/vuex.JPG">
<meta property="og:updated_time" content="2020-01-13T15:25:17.642Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Vue全家桶">
<meta name="twitter:description" content="vue全家桶也就是1.脚手架、2.路由、3.状态管理、4.请求工具~。">
<meta name="twitter:image" content="http://yoursite.com/images/vue-family/vue-cli.png">
    
        <link rel="alternate" type="application/atom+xml" title="小方块 - hhardyy.com" href="/atom.xml">
    
    <link rel="shortcut icon" href="/hardyfavicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/paulGraham.jpg)">
      <div class="brand" style="background-color:#4154b2">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">BingZhenhuang</h5>
          <a href="mailto:huangbingzhen@hhardyy.com" title="huangbingzhen@hhardyy.com" class="mail">huangbingzhen@hhardyy.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                所有文章
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/HHardyy" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://juejin.im/user/59a26f926fb9a02487553b04"  >
                <i class="icon icon-lg icon-pencil"></i>
                掘金-圳
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://segmentfault.com/u/hhardyy"  >
                <i class="icon icon-lg icon-comments"></i>
                Segmentfault
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://codepen.io/HHardyy/" target="_blank" >
                <i class="icon icon-lg icon-codepen"></i>
                Codepen
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/ZhenSolive/find.html" target="_blank" >
                <i class="icon icon-lg icon-globe"></i>
                原生直播间
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/hero/judge.html" target="_blank" >
                <i class="icon icon-lg icon-cloud"></i>
                原生小悟空
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/airPlay/HHardyy_PC.html" target="_blank" >
                <i class="icon icon-lg icon-camera"></i>
                原生飞机大战（PC）
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://www.freecodecamp.cn/hhardyy" target="_blank" >
                <i class="icon icon-lg icon-leaf"></i>
                Freecodecamp
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/友情链接"  >
                <i class="icon icon-lg icon-link"></i>
                友链
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">Vue全家桶</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">Vue全家桶</h1>
        <h5 class="subtitle">
            
                <time datetime="2018-06-10T08:02:10.000Z" itemprop="datePublished" class="page-time">
  2018-06-10
</time>


            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#vue-cli"><span class="post-toc-number">1.</span> <span class="post-toc-text">vue-cli</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#vue-router"><span class="post-toc-number">2.</span> <span class="post-toc-text">vue-router</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#router-view以及router-link"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">router-view以及router-link</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#router-view："><span class="post-toc-number">2.2.</span> <span class="post-toc-text">router-view：</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#router-link"><span class="post-toc-number">2.3.</span> <span class="post-toc-text">router-link</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#to-””"><span class="post-toc-number">2.3.1.</span> <span class="post-toc-text">:to=””</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#tag-”li”"><span class="post-toc-number">2.3.2.</span> <span class="post-toc-text">tag=”li”</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#replace"><span class="post-toc-number">2.3.3.</span> <span class="post-toc-text">replace</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#打包刷新404"><span class="post-toc-number">2.3.4.</span> <span class="post-toc-text">打包刷新404</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#new一个Router的时候里面的一些参数"><span class="post-toc-number">2.3.5.</span> <span class="post-toc-text">new一个Router的时候里面的一些参数</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#路由的参数"><span class="post-toc-number">2.3.6.</span> <span class="post-toc-text">路由的参数</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#路由跳转的过渡效果可以通过transition实现"><span class="post-toc-number">2.3.7.</span> <span class="post-toc-text">路由跳转的过渡效果可以通过transition实现</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#路由传参"><span class="post-toc-number">2.3.8.</span> <span class="post-toc-text">路由传参</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#命名视图"><span class="post-toc-number">2.3.9.</span> <span class="post-toc-text">命名视图</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#全局导航的路由钩子"><span class="post-toc-number">2.3.10.</span> <span class="post-toc-text">全局导航的路由钩子</span></a></li></ol></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#vuex"><span class="post-toc-number">3.</span> <span class="post-toc-text">vuex</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#分模块处理store"><span class="post-toc-number">3.1.</span> <span class="post-toc-text">分模块处理store</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#分模块随便写了一个简单易懂的demo"><span class="post-toc-number">3.2.</span> <span class="post-toc-text">分模块随便写了一个简单易懂的demo</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#axios"><span class="post-toc-number">4.</span> <span class="post-toc-text">axios</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#post请求"><span class="post-toc-number">4.1.</span> <span class="post-toc-text">post请求</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#请求多个并发"><span class="post-toc-number">4.2.</span> <span class="post-toc-text">请求多个并发</span></a></li></ol></li></ol>
        </nav>
    </aside>
    
<article id="post-Vue全家桶"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">Vue全家桶</h1>
        <div class="post-meta">
            <time class="post-time" title="2018-06-10 16:02:10" datetime="2018-06-10T08:02:10.000Z"  itemprop="datePublished">2018-06-10</time>

            


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <p>vue全家桶也就是1.脚手架、2.路由、3.状态管理、4.请求工具~。</p>
<p><iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width="330" height="86" src="//music.163.com/outchain/player?type=2&id=559931973&auto=0&height=66"></iframe><br><a id="more"></a><br>Vue是一套用于构建用户界面的MVVM渐进式框架。与其它大型框架不同的是，Vue的核心库只关注视图层，数据驱动，目标是通过尽可能简单的API实现响应的数据绑定和组合的视图组件，不再是传统的操作dom进行界面控制，当然也不是说完全不操作dom，必要的时候还是可以操作的，同时组件化的方式让项目维护起来特别的爽，Vue便于与第三方库或既有项目整合，当与单文件组件和Vue生态系统支持的库结合使用时，Vue也完全能够为复杂的单页应用程序提供驱动。</p>
<h2 id="vue-cli"><a href="#vue-cli" class="headerlink" title="vue-cli"></a>vue-cli</h2><p>也叫vue脚手架工具，脚手架是为了保证各施工过程顺利进行而搭设的工作平台，顾名思义在vue中是用于快速构建vue项目的工具，构建的大致步骤如下：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"># 安装vue脚手架</div><div class="line">npm install -g vue-cli</div><div class="line"></div><div class="line"># 安装webpack模板，testproject=项目名</div><div class="line">vue init webpack testproject</div><div class="line"></div><div class="line"># 安装项目需要的依赖包 </div><div class="line">npm install</div><div class="line"></div><div class="line"># 运行项目</div><div class="line">npm run dev</div></pre></td></tr></table></figure></p>
<p>这样就可以跑出一个初始化的vue项目，拖到sublime就可以看到各目录文件<br><figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="/images/vue-family/vue-cli.png" alt="“构建的项目目录”" title="">
                </div>
                <div class="image-caption">“构建的项目目录”</div>
            </figure><br>这个目录的意思就是<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line"><span class="number">1</span>、build <span class="comment">//项目构建(webpack)相关代码</span></div><div class="line"># build.js=生产环境构建代码</div><div class="line"># check-versions.js=检查node&amp;npm等版本</div><div class="line"># utils.js=构建配置公用工具</div><div class="line"># vue-loader.conf.js=vue加载器</div><div class="line"># webpack.base.conf.js=webpack基础环境配置</div><div class="line"># webpack.dev.conf.js=webpack开发环境配置包括构建本地服务器</div><div class="line"># webpack.prod.conf.js=webpack生产环境配置</div><div class="line"><span class="number">2</span>、config <span class="comment">//项目开发环境配置相关代码</span></div><div class="line"># dev.env.js=开发环境变量</div><div class="line"># index.js=项目一些配置变量，上线时候的接口可以在这里配置</div><div class="line"># prod.env.js=生产环境变量</div><div class="line"><span class="number">3</span>、node_modules <span class="comment">//项目所有依赖包的安装文件夹</span></div><div class="line"><span class="number">4</span>、src <span class="comment">//源码目录，也就是你写的代码在这里            5</span></div><div class="line"># assets=资源目录（style、iconfonts、公用的varrybel.styl等）</div><div class="line"># common=我自己建的用于存放公用组件的地方</div><div class="line"># router=路由</div><div class="line"># App.vue=项目入口文件</div><div class="line"># main.js=程序入口文件</div><div class="line"><span class="number">5</span>、<span class="keyword">static</span> <span class="comment">//静态文件，放一些图片，json数据等</span></div><div class="line"><span class="number">6</span>、.babelrc <span class="comment">//ES6语法编译配置</span></div><div class="line"><span class="number">7</span>、.editorconfig <span class="comment">//定义代码格式</span></div><div class="line"><span class="number">8</span>、.gitignore <span class="comment">//git上传的时候不希望上传的文件格式</span></div><div class="line"><span class="number">9</span>、 index.html <span class="comment">//入口页</span></div><div class="line"><span class="number">10</span>、package.json <span class="comment">//项目基本信息and这个项目所需要的依赖包，npm install的时候会根据这个去安装</span></div><div class="line"><span class="number">11</span>、README.md <span class="comment">//项目的一些说明</span></div></pre></td></tr></table></figure></p>
<h2 id="vue-router"><a href="#vue-router" class="headerlink" title="vue-router"></a>vue-router</h2><p>前端路由，就是指向的意思，类似a标签，当我点击某个地方或者链接时显示相应的内容，即Home =&gt; home 内容， detail =&gt; detail。</p>
<p>路由中有三个基本的概念 route, routes, router<br>1、route，一条路由，home =&gt; home是一条路由，detail =&gt; detail是另一条路由。<br>2、routes 一组路由，就是一条一条路由形成一个数组。[{home =&gt;home}， { detail =&gt; detail}]<br>3、router 一个机制，用来管理路由。routes只是定义了一组静止的路由，当发生请求的时候，也就是当用户触发home的时候，router会到routes中去查找相对应的相对应的内容进行展示<br>构造一组路由：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> routes = [</div><div class="line">  &#123; <span class="attr">path</span>: <span class="string">'/home'</span>, <span class="attr">component</span>: Home &#125;,</div><div class="line">  &#123; <span class="attr">path</span>: <span class="string">'/detail'</span>, <span class="attr">component</span>: Detail&#125;</div><div class="line">]</div></pre></td></tr></table></figure></p>
<p>用构造函数new vueRouter()构建一个router，然后传入routes进行管理：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter(&#123;</div><div class="line">      routes <span class="comment">//key值和value值相等的时候可以只写key值</span></div><div class="line">&#125;)</div></pre></td></tr></table></figure></p>
<p>注入根实例之后就可以使用了<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> app = <span class="keyword">new</span> Vue(&#123;</div><div class="line">  router</div><div class="line">&#125;).$mount(<span class="string">'#app'</span>)</div></pre></td></tr></table></figure></p>
<p>举例动态路由<br>安装方法移步<a href="https://router.vuejs.org/installation.html#direct-download-cdn" target="_blank" rel="external">vue官网</a>，这是我用的例子，大概结构就是有一个详情页的列表显示不同地方的旅游景点，我需要查看某个列表项的详情，这时候就用到了路由帮我带参数跳转过去，然后看到相应列表项的详情。<br>列表项组件<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">&lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">"hot-title"</span>&gt;推荐信息&lt;<span class="regexp">/div&gt;</span></div><div class="line"><span class="regexp">	&lt;ul&gt;</span></div><div class="line"><span class="regexp">		&lt;router-link</span></div><div class="line"><span class="regexp">		tag="li"</span></div><div class="line"><span class="regexp">		class="item border-bottom" </span></div><div class="line"><span class="regexp">		v-for="item of recommend" </span></div><div class="line"><span class="regexp">		:key="item.id"</span></div><div class="line"><span class="regexp">		:to="'/</span>detail/<span class="string">'+item.id"</span></div><div class="line"><span class="string">		&gt;</span></div><div class="line"><span class="string">		&lt;img class="item-img" :src="item.imgUrl" alt=""&gt;</span></div><div class="line"><span class="string">			&lt;div class="item-info"&gt;</span></div><div class="line"><span class="string">				&lt;p class="item-title"&gt;&#123;&#123;item.title&#125;&#125;&lt;/p&gt;</span></div><div class="line"><span class="string">				&lt;p class="item-desc"&gt;&#123;&#123;item.desc&#125;&#125;&lt;/p&gt;</span></div><div class="line"><span class="string">				&lt;button class="item-button"&gt;查看详情&lt;/button&gt;</span></div><div class="line"><span class="string">			&lt;/div&gt;</span></div><div class="line"><span class="string">		&lt;/router-link&gt;</span></div><div class="line"><span class="string">	&lt;/ul&gt;</span></div><div class="line"><span class="string">&lt;/div&gt;</span></div></pre></td></tr></table></figure></p>
<p>router目录下，index.js文件里头的写法，这是定义路径到组件的映射<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></div><div class="line"><span class="keyword">import</span> Router <span class="keyword">from</span> <span class="string">'vue-router'</span></div><div class="line"><span class="keyword">import</span> Home <span class="keyword">from</span> <span class="string">'@/pages/home/home'</span></div><div class="line"><span class="keyword">import</span> City <span class="keyword">from</span> <span class="string">'@/pages/city/city'</span></div><div class="line"><span class="keyword">import</span> Detail <span class="keyword">from</span> <span class="string">'@/pages/detail/detail'</span></div><div class="line"></div><div class="line">Vue.use(Router)</div><div class="line"></div><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">new</span> Router(&#123;</div><div class="line">  routes: [</div><div class="line">    &#123;</div><div class="line">      path: <span class="string">'/'</span>,   </div><div class="line">      name: <span class="string">'Home'</span>,</div><div class="line">      component: Home</div><div class="line">    &#125;，</div><div class="line">    &#123;</div><div class="line">      path:<span class="string">'/detail/:id'</span>,</div><div class="line">      name:<span class="string">"Detail"</span>,</div><div class="line">      component:Detail</div><div class="line">    &#125;</div><div class="line">  ],</div><div class="line">  scrollBehavior (to, <span class="keyword">from</span>, savedPosition) &#123;</div><div class="line">      <span class="keyword">return</span> &#123; <span class="attr">x</span>: <span class="number">0</span>, <span class="attr">y</span>: <span class="number">0</span> &#125;</div><div class="line">  &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure></p>
<p>可以参照<a href="https://github.com/HHardyy/Vue2.0-Travel-App" target="_blank" rel="external">我这个项目</a>的玩法</p>
<h3 id="router-view以及router-link"><a href="#router-view以及router-link" class="headerlink" title="router-view以及router-link"></a>router-view以及router-link</h3><h3 id="router-view："><a href="#router-view：" class="headerlink" title="router-view："></a>router-view：</h3><p>也就是项目入口文件里的<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">&lt;template&gt;</div><div class="line">  &lt;div id=<span class="string">"app"</span>&gt;</div><div class="line">  	&lt;keep-alive exclude=<span class="string">"detail"</span>&gt;</div><div class="line">      &lt;router-view/&gt;</div><div class="line">	&lt;<span class="regexp">/keep-alive&gt;</span></div><div class="line"><span class="regexp">  &lt;/</span>div&gt;</div><div class="line">&lt;<span class="regexp">/template&gt;</span></div></pre></td></tr></table></figure></p>
<p>是个很关键的组件，它就是用来渲染匹配到的路由的。 我这里搭配使用了keep-alive，keep-alive可以缓存数据，这样不至于重新渲染路由组件的时候，组件状态不被保存，也就是说从这个组件跳到其他组件再跳回来的的时候这个组件还是之前的状态</p>
<h3 id="router-link"><a href="#router-link" class="headerlink" title="router-link"></a>router-link</h3><p>router-link是一个组件，它默认会被渲染成一个带有链接的a标签，通过to属性指定链接地址，被选中的router-link将自动添加一个class属性值.router-link-active。<br>router-link的一些配置项，用法可以看上面简单的例子</p>
<h4 id="to-””"><a href="#to-””" class="headerlink" title=":to=””"></a>:to=””</h4><p>使用路由必须设置的属性，否则路由无法生效，可以是一个字符串也可以是一个描述目标位置的对象。</p>
<h4 id="tag-”li”"><a href="#tag-”li”" class="headerlink" title="tag=”li”"></a>tag=”li”</h4><p>一般router-link默认被渲染成a标签，tag属性可以用来设置router-link会被渲染成什么标签</p>
<h4 id="replace"><a href="#replace" class="headerlink" title="replace"></a>replace</h4><p>是个Boolean值，默认是false。当replace设置为true，那么导航不会留下history记录，点击浏览器回退按钮不会再回到这个路由。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;router-link to=<span class="string">"xxx"</span> replace&gt;&lt;/router-link&gt;</div></pre></td></tr></table></figure></p>
<h4 id="打包刷新404"><a href="#打包刷新404" class="headerlink" title="打包刷新404"></a>打包刷新404</h4><p>有时候你打包了一个dist放到服务器时访问好好的，突然用户刷新了一下页面，项目就404了，原因是前端项目的路由默认使用的mode是hash，也就是<a href="http://192.168.1.104:8080/#/app这样的类型，当然这也不利于做服务端渲染，hash一般是用来做定位的，而不是用来做路由的记录，而且hash路由不会被搜索引擎解析，所以也不利于seo优化，正常来说显示应该是http://192.168.1.104:8080/app,这个modo就是history，所以原因就是前端使用了historyurl之后是一个完整的资源路径，默认情况下服务端要进行匹配然后进行处理才能真正完成一个请求的操作，但是这在前端是没有问题的，如果手动刷新了浏览器，这个请求就会发送到服务端，服务端如果没有做任何处理，就会出现找不到资源或者404错误。" target="_blank" rel="external">http://192.168.1.104:8080/#/app这样的类型，当然这也不利于做服务端渲染，hash一般是用来做定位的，而不是用来做路由的记录，而且hash路由不会被搜索引擎解析，所以也不利于seo优化，正常来说显示应该是http://192.168.1.104:8080/app,这个modo就是history，所以原因就是前端使用了historyurl之后是一个完整的资源路径，默认情况下服务端要进行匹配然后进行处理才能真正完成一个请求的操作，但是这在前端是没有问题的，如果手动刷新了浏览器，这个请求就会发送到服务端，服务端如果没有做任何处理，就会出现找不到资源或者404错误。</a><br>开发环境解决方案：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> devServer = &#123;</div><div class="line">    historyApiFallback:&#123;</div><div class="line">        index:<span class="string">'/index.html'</span></div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>生产环境解决方案：<br>在服务端做匹配相关资源的配置，因为history模式下<a href="http://192.168.1.104:8080/app，会默认访问http://192.168.1.104:8080下面的资源" target="_blank" rel="external">http://192.168.1.104:8080/app，会默认访问http://192.168.1.104:8080下面的资源</a></p>
<h4 id="new一个Router的时候里面的一些参数"><a href="#new一个Router的时候里面的一些参数" class="headerlink" title="new一个Router的时候里面的一些参数"></a>new一个Router的时候里面的一些参数</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">mode:<span class="string">'hash'</span>                     <span class="comment">//默认hash，hash||history</span></div><div class="line">linkActiveClass:<span class="string">'active-link'</span>              <span class="comment">//路由被点击</span></div><div class="line">linkExactActiveClass:<span class="string">'exact-active-link'</span>   <span class="comment">//完整匹配路由被点击</span></div><div class="line">scrollBehavior(to,form,savedPosition)&#123;</div><div class="line">    <span class="keyword">if</span>(savePosition)&#123;</div><div class="line">        <span class="keyword">return</span> savePosition</div><div class="line">    &#125;<span class="keyword">else</span>&#123;</div><div class="line">        <span class="keyword">return</span> &#123;<span class="attr">x</span>:<span class="number">0</span>,<span class="attr">y</span>:<span class="number">0</span>&#125;</div><div class="line">    &#125;</div><div class="line">&#125;                                <span class="comment">//跳到另一个路由的时候记录当前路由的滚动条的位置，跳回来的时候回到原位</span></div><div class="line">fallback:<span class="literal">true</span>       <span class="comment">//有些浏览器不支持history模式的时候是否自动转换成hash</span></div><div class="line">parseQuery(query)&#123;&#125;   <span class="comment">//将路由后面跟着的query比如?aa=xx,bb=yy转成对象</span></div><div class="line">stringifyQuery(obj)&#123;&#125;  <span class="comment">//将路由后面跟着的query比如?aa=xx,bb=yy转成字符串</span></div></pre></td></tr></table></figure>
<h4 id="路由的参数"><a href="#路由的参数" class="headerlink" title="路由的参数"></a>路由的参数</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> Login <span class="keyword">from</span> <span class="string">'login.vue'</span></div><div class="line"><span class="keyword">import</span> Loginchildren <span class="keyword">from</span> <span class="string">'loginchildren.vue'</span></div><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> [</div><div class="line">    &#123;</div><div class="line">        path:<span class="string">'/'</span>              <span class="comment">//路径</span></div><div class="line">        name:<span class="string">'login'</span>      <span class="comment">//名字</span></div><div class="line">        component:Login   <span class="comment">//路由对应跳转组件</span></div><div class="line">        meta:&#123;            <span class="comment">//保存路由信息，可以在这里添加一些利于seo的东西</span></div><div class="line">            title:<span class="string">'hardy login page'</span></div><div class="line">            description:<span class="string">'...'</span></div><div class="line">            .....</div><div class="line">        &#125;</div><div class="line">        children:[    <span class="comment">//子路由</span></div><div class="line">           &#123;</div><div class="line">              path:<span class="string">'loginchildren'</span>,</div><div class="line">              component:Loginchildren</div><div class="line">           &#125;</div><div class="line">        ]</div><div class="line">    &#125;</div><div class="line">]</div></pre></td></tr></table></figure>
<p>路由命名的好处在于可以在页面上通过<router-link :to="{name:'login'}"></router-link>直接访问，路由渲染需要一个<router-view>，子路由也一样，只是不同的是比如在login下面建子路由，那么就要在login的组件里面需要渲染子路由的地方加上<router-view>  </router-view></router-view></p>
<h4 id="路由跳转的过渡效果可以通过transition实现"><a href="#路由跳转的过渡效果可以通过transition实现" class="headerlink" title="路由跳转的过渡效果可以通过transition实现"></a>路由跳转的过渡效果可以通过transition实现</h4><p>比如<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">&lt;transition name=<span class="string">"fade"</span>&gt;</div><div class="line">   &lt;router-view/&gt;</div><div class="line">&lt;<span class="regexp">/transition&gt;</span></div><div class="line"><span class="regexp">.fade-enter-active,.fade-leave-active</span></div><div class="line"><span class="regexp">  transition:opacity .5s</span></div><div class="line"><span class="regexp">.fade-enter,.fade-leave-to</span></div><div class="line"><span class="regexp">  opacity:0</span></div></pre></td></tr></table></figure></p>
<p>这样就有一个缓慢进入的效果</p>
<h4 id="路由传参"><a href="#路由传参" class="headerlink" title="路由传参"></a>路由传参</h4><p>比如要传一个id到index<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&#123;</div><div class="line">    path:<span class="string">'/index/:id'</span></div><div class="line">    component:Index</div><div class="line">&#125;</div><div class="line">&lt;router-link to=<span class="string">"/index/111"</span>&gt;首页&lt;<span class="regexp">/router-link&gt;</span></div></pre></td></tr></table></figure></p>
<p>然后可以在index组件的mounted的时候打印出传过来的id,console.log(this.$route.params),还有一种方法就是<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">&#123;</div><div class="line">    path:<span class="string">'/index/:id'</span></div><div class="line">    props:<span class="literal">true</span>                  <span class="comment">//将id作为props传入，这样就不需要用this.$route取值，而是直接props:['id']</span></div><div class="line">    component:Index</div><div class="line">&#125;</div><div class="line">props可以自定义，比如id:<span class="number">222</span></div><div class="line">&#123;</div><div class="line">    path:<span class="string">'/index/:id'</span></div><div class="line">    props:&#123;</div><div class="line">       id:<span class="string">'222'</span></div><div class="line">    &#125;                 </div><div class="line">    component:Index</div><div class="line">&#125;</div><div class="line">或者根据query来定义</div><div class="line">&#123;</div><div class="line">    path:<span class="string">'/index/:id'</span></div><div class="line">    props:<span class="function">(<span class="params">route</span>)=&gt;</span>(&#123;<span class="attr">id</span>:route.query.xxx&#125;)    <span class="comment">//route等于this.$route拿到的值            </span></div><div class="line">    component:Index</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h4 id="命名视图"><a href="#命名视图" class="headerlink" title="命名视图"></a>命名视图</h4><p>场景：同一个页面不同的部分显示同一个路由渲染，项目中场景就是有三个导航栏需要切换，顶部，侧边等…<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"> &lt;transition name=<span class="string">"fade"</span>&gt;</div><div class="line">   &lt;router-view/&gt;</div><div class="line">&lt;<span class="regexp">/transition&gt;</span></div><div class="line"><span class="regexp">...</span></div><div class="line"><span class="regexp">&lt;router-view name="namerouter"/</span>&gt;</div><div class="line">然后在route中这么配置</div><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> [</div><div class="line">    &#123;</div><div class="line">        path:<span class="string">'/'</span>              <span class="comment">//路径</span></div><div class="line">        name:<span class="string">'login'</span>      <span class="comment">//名字</span></div><div class="line">        components: &#123;</div><div class="line">            <span class="keyword">default</span>:Login     <span class="comment">//默认组件</span></div><div class="line">            namerouter:Login1   <span class="comment">//name="namerouter"渲染组件</span></div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">]</div></pre></td></tr></table></figure></p>
<h4 id="全局导航的路由钩子"><a href="#全局导航的路由钩子" class="headerlink" title="全局导航的路由钩子"></a>全局导航的路由钩子</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> router = createRouter()</div><div class="line">router.beforEach(<span class="function">(<span class="params">to,<span class="keyword">from</span>,next</span>)=&gt;</span>&#123;</div><div class="line">    <span class="built_in">console</span>.log(<span class="string">'before each incoked'</span>)</div><div class="line">    next()</div><div class="line">&#125;)</div><div class="line">router.beforeResolve(<span class="function">(<span class="params">to,<span class="keyword">from</span>,next</span>)=&gt;</span>&#123;</div><div class="line">    <span class="built_in">console</span>.log(<span class="string">'before resolve'</span>)</div><div class="line">    next()</div><div class="line">&#125;)</div><div class="line">router.afterEach(<span class="function">(<span class="params">to,<span class="keyword">from</span></span>)=&gt;</span>&#123;</div><div class="line">    <span class="built_in">console</span>.log(<span class="string">'after each'</span>)</div><div class="line">&#125;)</div></pre></td></tr></table></figure>
<p>这几个钩子可以用来做路由跳转校验,例如当路由是login的时候跳转<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">router.beforEach(<span class="function">(<span class="params">to,<span class="keyword">from</span>,next</span>)=&gt;</span>&#123;</div><div class="line">    <span class="keyword">if</span>(to.fullPath === <span class="string">'/login'</span>)&#123;</div><div class="line">        next()   </div><div class="line">    &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure></p>
<p>路有内部也可以增加钩子<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> [</div><div class="line">    &#123;</div><div class="line">        path:<span class="string">'/'</span>              <span class="comment">//路径</span></div><div class="line">        name:<span class="string">'login'</span>      <span class="comment">//名字</span></div><div class="line">        components: &#123;</div><div class="line">            <span class="keyword">default</span>:Login     <span class="comment">//默认组件</span></div><div class="line">            namerouter:Login1   <span class="comment">//name="namerouter"渲染组件</span></div><div class="line">        &#125;</div><div class="line">        beforeEnter(to,<span class="keyword">from</span>,next)&#123;    <span class="comment">//进入这个路由之前被调用，执行顺序在router.beforEach之后，router.beforeResolve之前</span></div><div class="line">            next()</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">]</div></pre></td></tr></table></figure></p>
<p>组件内部也存在路由的钩子<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">&lt;script&gt;</div><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</div><div class="line">    beforRouteEnter(to,<span class="keyword">from</span>,next)&#123;</div><div class="line">        <span class="built_in">console</span>.log(<span class="string">'before enter'</span>)</div><div class="line">        next()</div><div class="line">    &#125;,</div><div class="line">    beforeRouteUpdate(to,<span class="keyword">from</span>,next)&#123;   <span class="comment">//同一个组件在不同路由跳转的时候触发</span></div><div class="line">        <span class="built_in">console</span>.log(<span class="string">'uodate enter'</span>)</div><div class="line">        next()    <span class="comment">//如果这个路由带了参数，可以在跳转的时候做判断 next(vm=&gt;&#123;console.log(vm.id)&#125;)，这样可以节省一个watch:&#123; id(val,oldval)&#123;&#125;&#125;</span></div><div class="line">    &#125;,</div><div class="line">    beforeRouteLeave(to,<span class="keyword">from</span>,next)&#123;   <span class="comment">//路由离开的时候触发，如果用户填了一个巨大的表单，离开的时候可以提示他</span></div><div class="line">        <span class="built_in">console</span>.log(<span class="string">'leave enter'</span>)</div><div class="line">        <span class="keyword">if</span>(global.comfirm(<span class="string">'are you sure?'</span>))&#123;</div><div class="line">            next()</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line">&lt;<span class="regexp">/script&gt;</span></div></pre></td></tr></table></figure></p>
<p>执行顺序就是，跳一个路由的时候<br>befor each invoked=&gt;app route before enter=&gt;before enter=&gt;before resolve invoked=&gt;after each</p>
<h2 id="vuex"><a href="#vuex" class="headerlink" title="vuex"></a>vuex</h2><p>Vuex是Vue.js应用程序的状态管理模式+库。作为应用程序中所有组件的集中存储区，并且规则确保只能以可预测的方式对状态进行变更。也就是可以全局数据管理，其实就是一个单向数据的改变流程。</p>
<p>vuex核心概念：<br>1、state：驱动应用的数据源<br>2、getters：为组件提供经过处理的数据源<br>3、mutations：用来更改state的唯一标准方式<br>4、actions：组件通过调用actions来提交mutation<br>5、modules：每个module都有自己的state、getters、mutations、actions实现大型应用中业务模块数据的分支治</p>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="/images/vue-family/vuex.JPG" alt="“根据官网提供的vuex生命周期视图”" title="">
                </div>
                <div class="image-caption">“根据官网提供的vuex生命周期视图”</div>
            </figure>
<p>1、state：类似vue对象的data, 用来存放数据以及状态。存放的数据为响应式，如果数据改变，那么依赖数据的组件也会发生相应的改变。<br>获取state的两种方式例子：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="number">1.</span>store.getters[<span class="string">'xxx'</span>]</div><div class="line"><span class="number">2.</span> ...mapGetters(&#123;</div><div class="line">        UserInfo: <span class="string">'xxx'</span>, <span class="comment">// 用户信息</span></div><div class="line">        menuList: <span class="string">'xxx'</span>, <span class="comment">// approve 运价审批</span></div><div class="line">        RateUserInfo: <span class="string">'xxxx'</span> <span class="comment">// Rate用户信息</span></div><div class="line">   &#125;)</div><div class="line">可以通过mapState把全局的state和getters映射到当前组件的computed计算属性中。</div></pre></td></tr></table></figure></p>
<p>2、actions<br>通过store.dispatch方法触发,action支持异步调用（可以调用api），mutation只支持操作同步，并且action提交的是mutation，而不是直接变更状态。Action函数接受一个与store 实例具有相同方法和属性的context对象，因此可以调用context.commit提交一个mutation，或者通过context.state和context.getters来获取state和getters。<br>3、mutation<br>每个mutation都有一个字符串的事件类型(type)和一个回调函数(handler)。这个回调函数就是我们实际进行状态更改的地方，并且它会接受state作为第一个参数。<br>4、Vuex允许在store中定义“getter”（可以认为是store的计算属性）。就像计算属性一样，getter的返回值会根据它的依赖被缓存起来，且只有当它的依赖值发生了改变才会被重新计算。</p>
<p>举例：为了方便在src目录下新建一个store目录，里头新建一个index.js,然后使用<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></div><div class="line"><span class="keyword">import</span> Vuex <span class="keyword">from</span> <span class="string">'vuex'</span><span class="comment">//引入Vuex</span></div><div class="line"></div><div class="line">Vue.use(Vuex)<span class="comment">//让vue使用vuex</span></div><div class="line"></div><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">new</span> Vuex.Store(&#123;</div><div class="line">	state:&#123;    <span class="comment">//全局数据</span></div><div class="line">		key:<span class="string">'value'</span></div><div class="line">	&#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure></p>
<p>写完这些之后在main.js里头写<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">'./store'</span></div><div class="line"></div><div class="line">创建根vue实例的时候和使用vuetouter一样，当key=value的时候只需要写一个就好了</div><div class="line"><span class="keyword">new</span> Vue(&#123;</div><div class="line">  store</div><div class="line">&#125;)</div></pre></td></tr></table></figure></p>
<p>这样子它就会将数据 自动派发到每个子组件，比如子组件需要使用key的值，可以直接，就ko了。</p>
<p>假如要改变多个页面的额数据，在上面的基础之上根据官网提供的vuex生命周期触发点击事件的组件里面写这样的方法<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">methods:&#123;</div><div class="line">	handelCheckHotcity(city)&#123;</div><div class="line">		<span class="keyword">this</span>.$store.dispatch(<span class="string">'changeCity'</span>,city)</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>store文件下的index.js的公共数据<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></div><div class="line"><span class="keyword">import</span> Vuex <span class="keyword">from</span> <span class="string">'vuex'</span></div><div class="line"></div><div class="line">Vue.use(Vuex)</div><div class="line"></div><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">new</span> Vuex.Store(&#123;</div><div class="line">	state:&#123;</div><div class="line">		city:<span class="string">'香港'</span></div><div class="line">	&#125;,</div><div class="line">	actions:&#123;</div><div class="line">		changeCity(ctx,city)&#123;       <span class="comment">//ctx是上下文，city是传入的参数</span></div><div class="line">			ctx.commit(<span class="string">'changeCity'</span>,city)</div><div class="line">		&#125;</div><div class="line">	&#125;,</div><div class="line">	mutations:&#123;</div><div class="line">		changeCity(state,city)&#123;</div><div class="line">			state.city=city</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure></p>
<p>组件也可以不用先dispatch触发changeCity方法，可以直接commit<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">this</span>.$store.commit(<span class="string">'changeCity'</span>,city)</div></pre></td></tr></table></figure></p>
<p>store文件下的index.js公共数据actions，直接写mutations对象，然后<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">changeCity(state,city)&#123;</div><div class="line">	 state.city=city</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>vuex还有个好处就是可以在不同组件@click这个方法来对写了vuex逻辑的index.js文件里的数据进行修改<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">handelCheckHotcity(city)&#123;</div><div class="line">	<span class="keyword">this</span>.$store.dispatch(<span class="string">'changeCity'</span>,city)</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h3 id="分模块处理store"><a href="#分模块处理store" class="headerlink" title="分模块处理store"></a>分模块处理store</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</div><div class="line">   state,</div><div class="line">   mutations,</div><div class="line">   actions,</div><div class="line">   getters,</div><div class="line">   modules:&#123;</div><div class="line">      a:&#123;                  <span class="comment">//模块a</span></div><div class="line">          namespaced:<span class="literal">true</span>,   <span class="comment">//加入命名空间，独立出这个模块的命名空间</span></div><div class="line">          state:&#123;</div><div class="line">             value:<span class="number">1</span></div><div class="line">          &#125;,</div><div class="line">          mutations:&#123;      </div><div class="line">             updateVal:(state,val)&#123;</div><div class="line">                state.value = val </div><div class="line">             &#125;</div><div class="line">          &#125;, </div><div class="line">          actions:&#123;             <span class="comment">//这样声明的actions只会在当前作用域下面找，如果要去全局作用域下面找可以加上参数&#123;root:true&#125;</span></div><div class="line">             actionsFunc(&#123; state,commit,rootState &#125;)&#123;</div><div class="line">                commit(<span class="string">'updateText'</span>,rootState.value,&#123;<span class="attr">value</span>:<span class="literal">true</span>&#125;)</div><div class="line">             &#125;</div><div class="line">          &#125;,</div><div class="line">          getters:&#123;</div><div class="line">             valPlus:&#123;</div><div class="line">                <span class="keyword">return</span> value+<span class="number">1</span></div><div class="line">             &#125;</div><div class="line">          &#125;</div><div class="line">      &#125;,</div><div class="line">      b:&#123;                  <span class="comment">//模块b</span></div><div class="line">          state:&#123;</div><div class="line">             value:<span class="number">2</span></div><div class="line">          &#125;</div><div class="line">      &#125;</div><div class="line">   &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>分模块的时候拿state和不分模块的时候拿的方式不一样,区别就像这样<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">computed()&#123;</div><div class="line">    ...mapState(&#123;</div><div class="line">       val:<span class="function"><span class="params">state</span>=&gt;</span>state.value       <span class="comment">//不分模块的时候</span></div><div class="line">       val:<span class="function"><span class="params">state</span>=&gt;</span>state.a.value     <span class="comment">//分模块的时候</span></div><div class="line">    &#125;),</div><div class="line">    ...mapGetters([<span class="string">'valPlus'</span>,<span class="string">'a/valPlus'</span>])    <span class="comment">//valPlus可以在template里面直接用，但是a/valPlus这个东西不能直接用，所以可以改成Obj的形式’</span></div><div class="line"></div><div class="line">    ...mapGetters(&#123;                 <span class="comment">//真实环境中两个声明接受的值不会写一样，这里只是为了方便看清</span></div><div class="line">        valPlus:<span class="string">'valPlus'</span>,</div><div class="line">        valPlus:<span class="string">'a/valPlus'</span></div><div class="line">    &#125;)</div><div class="line">&#125;</div><div class="line">methods:&#123;</div><div class="line">    ...mapMutations([<span class="string">'updateVal'</span>,<span class="string">'a/updateVal'</span>])     <span class="comment">//[0]不分模块的时候，//[1]分模块的时候</span></div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>调用的时候也区分<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">mounted()&#123;</div><div class="line">    <span class="keyword">this</span>.updateVal(<span class="string">'111'</span>)          <span class="comment">//不分模块的时候</span></div><div class="line">    <span class="keyword">this</span>[<span class="string">'a/updateVal'</span>](<span class="string">'111'</span>)     <span class="comment">//分模块的时候    </span></div><div class="line">&#125;</div><div class="line">namespaced是命名空间的概念，和我们写c++一样在include一大堆的后面加上当前的命名空间namespace code</div></pre></td></tr></table></figure></p>
<p>调用actions的时候<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">mounted()&#123;</div><div class="line">    <span class="keyword">this</span>.actionsFunc()          <span class="comment">//全局的actions</span></div><div class="line">    <span class="keyword">this</span>[<span class="string">'a/actionsFunc'</span>]()      <span class="comment">//调用a模块下面的actionsFunc</span></div><div class="line">&#125;,</div><div class="line">methods:&#123;</div><div class="line">    ...mapActions([<span class="string">'actionsFunc'</span>])   <span class="comment">//全局的actions</span></div><div class="line">    ...mapActions([<span class="string">'a/actionsFunc'</span>])   <span class="comment">//调用a模块下面的actionsFunc</span></div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>动态注册模块，<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">store.registerModule(<span class="string">'c'</span>,&#123;</div><div class="line">   state:&#123;</div><div class="line">      value:<span class="number">2</span></div><div class="line">   &#125;</div><div class="line">&#125;)</div></pre></td></tr></table></figure></p>
<p>调用方式一样<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">computed:&#123;</div><div class="line">   ...mapState(&#123;</div><div class="line">       cvalue:<span class="function"><span class="params">state</span>=&gt;</span>state.c.value</div><div class="line">   &#125;)</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>解绑一个模块，<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">store.unregisterModule(<span class="string">'c'</span>)</div></pre></td></tr></table></figure></p>
<p>store里面的watch，这个方法和getters一样，只不过这个方法接收两个函数参数<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">store.watch(<span class="function">(<span class="params">state</span>)=&gt;</span>&#123;    <span class="comment">//watch接收两个函数，第一个里面可以watch state,当state改变之后触发第二个函数回调</span></div><div class="line">   state.value + <span class="number">1</span></div><div class="line">&#125;,(newValue)=&gt;&#123;</div><div class="line">   <span class="built_in">console</span>.log(newValue)   </div><div class="line">&#125;)</div></pre></td></tr></table></figure></p>
<p>store里面的subscribe,监听所有mutations<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">store.subscribe(<span class="function">(<span class="params">mutation,state</span>)=&gt;</span>&#123;</div><div class="line">    cosnole.log(mutation.type)   <span class="comment">//触发的mutations</span></div><div class="line">    <span class="built_in">console</span>.log(mutation.payload)  <span class="comment">//接收的参数</span></div><div class="line">&#125;)</div></pre></td></tr></table></figure></p>
<p>store里面的subscribeAction,监听所有actions<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">store.subscribeAction(<span class="function">(<span class="params">action,state</span>)=&gt;</span>&#123;</div><div class="line">    cosnole.log(action.type)   <span class="comment">//触发的actions</span></div><div class="line">    <span class="built_in">console</span>.log(action.payload)  <span class="comment">//接收的参数</span></div><div class="line">&#125;)</div></pre></td></tr></table></figure></p>
<p>store里面的plugins<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> ()=&gt;&#123;</div><div class="line">    <span class="keyword">const</span> store = <span class="keyword">new</span> Vuex.Store(&#123;</div><div class="line">        strict:isDev,             </div><div class="line">        state,</div><div class="line">        mutations,</div><div class="line">        getters,</div><div class="line">        actions,</div><div class="line">        plugins:[</div><div class="line">            (store)=&gt;&#123;</div><div class="line">                <span class="built_in">console</span>.log(<span class="string">'invoked'</span>)    <span class="comment">//会在store初始化的时候执行</span></div><div class="line">            &#125;</div><div class="line">        ]  </div><div class="line">    &#125;)</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>开启热更新的功能，就是说vuex的状态改变的时候需要刷新一下页面才看的到效果，所以vuex里面也提供了热更新的api<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> ()=&gt;&#123;</div><div class="line">    <span class="keyword">const</span> store = <span class="keyword">new</span> Vuex.Store(&#123;</div><div class="line">        strict:isDev,               <span class="comment">//开发环境开启严格模式，防止团队一些同学写出不规范的代码</span></div><div class="line">        state,</div><div class="line">        mutations,</div><div class="line">        getters,</div><div class="line">        actions  </div><div class="line">    &#125;)</div><div class="line">    <span class="keyword">if</span>(<span class="built_in">module</span>.hot)&#123;</div><div class="line">        <span class="built_in">module</span>.hot.accept([</div><div class="line">          <span class="string">'./state.js'</span>,</div><div class="line">          <span class="string">'./mutations.js'</span>,</div><div class="line">          <span class="string">'./actions.js'</span>,</div><div class="line">          <span class="string">'./getters.js'</span></div><div class="line">    ],()=&gt;&#123;</div><div class="line">        <span class="keyword">const</span> newState = <span class="built_in">require</span>(<span class="string">'./state.js'</span>).default</div><div class="line">        <span class="keyword">const</span> newMutations = <span class="built_in">require</span>(<span class="string">'./mutations.js'</span>).default</div><div class="line">        <span class="keyword">const</span> newGetters = <span class="built_in">require</span>(<span class="string">'./getters.js'</span>).default</div><div class="line">        <span class="keyword">const</span> newActions = <span class="built_in">require</span>(<span class="string">'./actions.js'</span>).default</div><div class="line"></div><div class="line">        store.hotUpdate(&#123;</div><div class="line">            state:newState,</div><div class="line">            mutations:newMutations,</div><div class="line">            getters:newGetters,</div><div class="line">            actions:newActions</div><div class="line">        &#125;)</div><div class="line">    &#125;)</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> store</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h3 id="分模块随便写了一个简单易懂的demo"><a href="#分模块随便写了一个简单易懂的demo" class="headerlink" title="分模块随便写了一个简单易懂的demo"></a>分模块随便写了一个简单易懂的demo</h3><p>src/store/index.js<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></div><div class="line"><span class="keyword">import</span> Vuex <span class="keyword">from</span> <span class="string">'vuex'</span></div><div class="line"><span class="keyword">import</span> todolist <span class="keyword">from</span> <span class="string">'./modules/todolist'</span></div><div class="line"></div><div class="line">Vue.use(Vuex)</div><div class="line"></div><div class="line"><span class="keyword">const</span> store =<span class="keyword">new</span> Vuex.Store(&#123;</div><div class="line">  modules: &#123;</div><div class="line">    todolist</div><div class="line">  &#125;</div><div class="line">&#125;)</div><div class="line"></div><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> store</div></pre></td></tr></table></figure></p>
<p>src/store/modules/todolist.js<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> state = &#123;</div><div class="line">  list: [<span class="string">'hardy1'</span>, <span class="string">'hardy2'</span>]</div><div class="line">&#125;</div><div class="line"><span class="keyword">const</span> mutations = &#123;</div><div class="line">  addValToList(state, data) &#123;</div><div class="line">    state.list.unshift(data)</div><div class="line">  &#125;,</div><div class="line">  remoValToList(state, index) &#123; </div><div class="line">    state.list.splice(index,<span class="number">1</span>)</div><div class="line">  &#125;</div><div class="line">&#125;</div><div class="line"><span class="keyword">const</span> actions = &#123;</div><div class="line">  addTolist(&#123; commit &#125;, data) &#123;</div><div class="line">    commit(<span class="string">'addValToList'</span>, data)</div><div class="line">  &#125;,</div><div class="line">  remTodolist(&#123; commit &#125;, index) &#123; </div><div class="line">    commit(<span class="string">'remoValToList'</span>,index)</div><div class="line">  &#125;</div><div class="line">&#125;</div><div class="line"><span class="keyword">const</span> getters = &#123;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123; state, mutations, actions, getters &#125;</div></pre></td></tr></table></figure></p>
<p>全局使用/App.vue<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">&lt;script&gt;</div><div class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">'./store/index'</span></div><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</div><div class="line">  name:<span class="string">'App'</span>,</div><div class="line">  store</div><div class="line">&#125;</div><div class="line">&lt;<span class="regexp">/script&gt;</span></div></pre></td></tr></table></figure></p>
<p>components/todolist.vue<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div></pre></td><td class="code"><pre><div class="line">&lt;template&gt;</div><div class="line">  &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">"todolist"</span>&gt;</div><div class="line">    &lt;div&gt;</div><div class="line">      &lt;input type=<span class="string">"text"</span> v-model=<span class="string">"inputVal"</span> /&gt;</div><div class="line">      &lt;input type=<span class="string">"button"</span> @click=<span class="string">"handelAdd"</span> value=<span class="string">"ADD"</span> /&gt;</div><div class="line">    &lt;<span class="regexp">/div&gt;</span></div><div class="line"><span class="regexp">    &lt;div&gt;</span></div><div class="line"><span class="regexp">      &lt;ul&gt;</span></div><div class="line"><span class="regexp">        &lt;li </span></div><div class="line"><span class="regexp">        v-for="(item,index) in list" </span></div><div class="line"><span class="regexp">        :key="item" </span></div><div class="line"><span class="regexp">        &gt;</span></div><div class="line"><span class="regexp">          &#123;&#123;item&#125;&#125;</span></div><div class="line"><span class="regexp">          &lt;span style="cursor:pointer" @click="handelRemove(index)"&gt;X&lt;/</span>span&gt;</div><div class="line">        &lt;<span class="regexp">/li&gt;</span></div><div class="line"><span class="regexp">      &lt;/u</span>l&gt;</div><div class="line">    &lt;<span class="regexp">/div&gt;</span></div><div class="line"><span class="regexp">  &lt;/</span>div&gt;</div><div class="line">&lt;<span class="regexp">/template&gt;</span></div><div class="line"><span class="regexp"></span></div><div class="line"><span class="regexp">&lt;script&gt;</span></div><div class="line"><span class="regexp">import &#123; mapActions, mapState &#125; from "vuex";</span></div><div class="line"><span class="regexp">export default &#123;</span></div><div class="line"><span class="regexp">  data() &#123;</span></div><div class="line"><span class="regexp">    return &#123;</span></div><div class="line"><span class="regexp">      inputVal: ""</span></div><div class="line"><span class="regexp">    &#125;;</span></div><div class="line"><span class="regexp">  &#125;,</span></div><div class="line"><span class="regexp">  computed: &#123;</span></div><div class="line"><span class="regexp">    ...mapState(&#123;</span></div><div class="line"><span class="regexp">      list: state =&gt; &#123;</span></div><div class="line"><span class="regexp">        return state.todolist.list;</span></div><div class="line"><span class="regexp">      &#125;</span></div><div class="line"><span class="regexp">    &#125;)</span></div><div class="line"><span class="regexp">  &#125;,</span></div><div class="line"><span class="regexp">  methods: &#123;</span></div><div class="line"><span class="regexp">    ...mapActions(["addTolist","remTodolist"]),</span></div><div class="line"><span class="regexp">    handelAdd() &#123;</span></div><div class="line"><span class="regexp">      this.addTolist(this.$data.inputVal);</span></div><div class="line"><span class="regexp">      this.$data.inputVal = "";</span></div><div class="line"><span class="regexp">    &#125;,</span></div><div class="line"><span class="regexp">    handelRemove(index) &#123;</span></div><div class="line"><span class="regexp">      this.remTodolist(index)</span></div><div class="line"><span class="regexp">    &#125;</span></div><div class="line"><span class="regexp">  &#125;</span></div><div class="line"><span class="regexp">&#125;;</span></div><div class="line"><span class="regexp">&lt;/</span>script&gt;</div></pre></td></tr></table></figure></p>
<h2 id="axios"><a href="#axios" class="headerlink" title="axios"></a>axios</h2><p>axios是一个基于Promise用于浏览器和nodejs的HTTP客户端，可以实现跨平台数据请求，node平台上可以发送http请求，<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">npm install axios --save</div></pre></td></tr></table></figure></p>
<p>了解过ajax的道友应该都懂了，这里我在mounted里面触发了一个methods里面的getDetailData方法，向接口请求数据来实现交互效果<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">getDetailData()&#123;</div><div class="line">	axios.get(<span class="string">'接口地址'</span>,&#123;</div><div class="line">		params:&#123;</div><div class="line">			id:<span class="keyword">this</span>.$route.params.id</div><div class="line">			&#125;</div><div class="line">		&#125;)</div><div class="line">		.then(<span class="function">(<span class="params">res</span>)=&gt;</span>&#123;</div><div class="line">			<span class="built_in">console</span>.log(res)</div><div class="line">		&#125;)</div><div class="line">		.catch(<span class="function">(<span class="params">err</span>)=&gt;</span>&#123;</div><div class="line">			<span class="built_in">console</span>.log(err)</div><div class="line">	    &#125;)</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h3 id="post请求"><a href="#post请求" class="headerlink" title="post请求"></a>post请求</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">axios.post(<span class="string">'/user'</span>, &#123;</div><div class="line">    name: <span class="string">'hhardyy'</span>,</div><div class="line">    sex: <span class="string">'male'</span></div><div class="line">  &#125;)</div><div class="line">  .then(<span class="function">(<span class="params">res</span>)=&gt;</span>&#123;</div><div class="line">	    <span class="built_in">console</span>.log(res)</div><div class="line">	&#125;)</div><div class="line">	.catch(<span class="function">(<span class="params">err</span>)=&gt;</span>&#123;</div><div class="line">		<span class="built_in">console</span>.log(err)</div><div class="line">	&#125;)</div></pre></td></tr></table></figure>
<h3 id="请求多个并发"><a href="#请求多个并发" class="headerlink" title="请求多个并发"></a>请求多个并发</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">getUserAccount</span>(<span class="params"></span>) </span>&#123;</div><div class="line">  <span class="keyword">return</span> axios.get(<span class="string">'/user/12345'</span>);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">getUserPermissions</span>(<span class="params"></span>) </span>&#123;</div><div class="line">  <span class="keyword">return</span> axios.get(<span class="string">'/user/12345/permissions'</span>);</div><div class="line">&#125;</div><div class="line"></div><div class="line">axios.all([getUserAccount(), getUserPermissions()])</div><div class="line">  .then(axios.spread(<span class="function"><span class="keyword">function</span> (<span class="params">acct, perms</span>) </span>&#123;</div><div class="line">    <span class="comment">// 两个请求现在都执行完成</span></div><div class="line">  &#125;));</div></pre></td></tr></table></figure>
<p>最近一直在熬夜，准确的说今年一直在熬夜ლ(′◉❥◉｀ლ)，渐渐觉得深夜才能创造灵感，不过白天眼睛痛~感觉都要开启永恒万花筒写轮眼了~<br><a href="https://www.kancloud.cn/yunye/axios/234845" target="_blank" rel="external">更多用法可以参照这里</a></p>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2020-01-13T15:25:17.642Z" itemprop="dateUpdated">2020-01-13 23:25:17</time>
</span><br>


        
        谢谢浏览，我会继续努力的，示例：<a href="/2018/06/10/Vue全家桶/" target="_blank" rel="external">http://yoursite.com/2018/06/10/Vue全家桶/</a>
        
    </div>
    <footer>
        <a href="http://yoursite.com">
            <img src="/img/avatar.jpg" alt="BingZhenhuang">
            BingZhenhuang
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/axios/">axios</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/store/">store</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/store分模块/">store分模块</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/vue-cli/">vue-cli</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/vue-router/">vue-router</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/vuex/">vuex</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/刷新404/">刷新404</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/路由/">路由</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://yoursite.com/2018/06/10/Vue全家桶/&title=《Vue全家桶》 — 小方块 - hhardyy.com&pic=http://yoursite.com/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://yoursite.com/2018/06/10/Vue全家桶/&title=《Vue全家桶》 — 小方块 - hhardyy.com&source=vue全家桶也就是1.脚手架、2.路由、3.状态管理、4.请求工具~。
" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://yoursite.com/2018/06/10/Vue全家桶/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Vue全家桶》 — 小方块 - hhardyy.com&url=http://yoursite.com/2018/06/10/Vue全家桶/&via=http://yoursite.com" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://yoursite.com/2018/06/10/Vue全家桶/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2018/07/01/Web安全之XSS、SQL注入/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">Web安全之XSS、SQL注入</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2018/06/10/Vue全家桶开发旅游app/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">Vue全家桶开发旅游app</h4>
      </a>
    </div>
  
</nav>



    














</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        🤠 请我喝可乐！
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content" style="width:50%">
        
        <div class="reward-code" style="text-align:center">
            <div style="width:300px;margin:0px auto;">
               <img id="rewardCode" style="width:50%;height:60%;display:block; margin:0px auto;" src="/img/alipay.jpg" alt="支付宝打赏二维码">
               <span style="display:inline-block; margin-bottom:20px;">0.88(支付宝 aliPay)</span>
               <img id="rewardCode" style="width:50%;height:60%;display:block; margin:0px auto;" src="/img/wechat.jpg" alt="微信打赏二维码">
               <span style="display:inline-block;">0.88(微信 weChat)</span>
            </div>
        </div>
    </div>
</div>



</div>

        <script>
!function(e,t,a){function n(){c(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"),o(),r()}function r(){for(var e=0;e<d.length;e++)d[e].alpha<=0?(t.body.removeChild(d[e].el),d.splice(e,1)):(d[e].y--,d[e].scale+=.004,d[e].alpha-=.013,d[e].el.style.cssText="left:"+d[e].x+"px;top:"+d[e].y+"px;opacity:"+d[e].alpha+";transform:scale("+d[e].scale+","+d[e].scale+") rotate(45deg);background:"+d[e].color+";z-index:99999");requestAnimationFrame(r)}function o(){var t="function"==typeof e.onclick&&e.onclick;e.onclick=function(e){t&&t(),i(e)}}function i(e){var a=t.createElement("div");a.className="heart",d.push({el:a,x:e.clientX-5,y:e.clientY-5,scale:1,alpha:1,color:s()}),t.body.appendChild(a)}function c(e){var a=t.createElement("style");a.type="text/css";try{a.appendChild(t.createTextNode(e))}catch(t){a.styleSheet.cssText=e}t.getElementsByTagName("head")[0].appendChild(a)}function s(){return"rgb("+~~(255*Math.random())+","+~~(255*Math.random())+","+~~(255*Math.random())+")"}var d=[];e.requestAnimationFrame=function(){return e.requestAnimationFrame||e.webkitRequestAnimationFrame||e.mozRequestAnimationFrame||e.oRequestAnimationFrame||e.msRequestAnimationFrame||function(e){setTimeout(e,1e3/60)}}(),n()}(window,document);
</script>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<script>
    function secondToDate(second) {
        if (!second) {
            return 0;
        }
        var time = new Array(0, 0, 0, 0, 0);
        if (second >= 365 * 24 * 3600) {
            time[0] = parseInt(second / (365 * 24 * 3600));
            second %= 365 * 24 * 3600;
        }
        if (second >= 24 * 3600) {
            time[1] = parseInt(second / (24 * 3600));
            second %= 24 * 3600;
        }
        if (second >= 3600) {
            time[2] = parseInt(second / 3600);
            second %= 3600;
        }
        if (second >= 60) {
            time[3] = parseInt(second / 60);
            second %= 60;
        }
        if (second > 0) {
            time[4] = second;
        }
        return time;
    }</script>
<script type="text/javascript" language="javascript">
    function setTime() {
        var create_time = Math.round(new Date(Date.UTC(2017, 08, 18, 11, 42, 23)).getTime() / 1000);
        var timestamp = Math.round((new Date().getTime() + 8 * 60 * 60 * 1000) / 1000);
        currentTime = secondToDate((timestamp - create_time));
        currentTimeHtml = 'Running：' + currentTime[0] + '年 ' + currentTime[1] + '天 '
                + currentTime[2] + '时 ' + currentTime[3] + '分 ' + currentTime[4]
                + '秒';
        document.getElementById("htmer_time").innerHTML = currentTimeHtml;
    }    setInterval(setTime, 1000);
</script>
<footer class="footer">
    <div class="top">
        

        <p>
          <span id="busuanzi_container_page_pv">
             [&nbsp;浏览量：&nbsp;<span id="busuanzi_value_page_pv"></span>&nbsp;]
          </span>
        </p>
    </div>
    <div class="bottom">
        <p>
        <span>BingZhenhuang &copy; 2017 - 2020</span>
            <span>
                
                Power by <a href="https://hhardyy.github.io/" target="_blank">zhen On August 8</a> 
            </span>
            <span id="htmer_time" "></span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://yoursite.com/2018/06/10/Vue全家桶/&title=《Vue全家桶》 — 小方块 - hhardyy.com&pic=http://yoursite.com/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://yoursite.com/2018/06/10/Vue全家桶/&title=《Vue全家桶》 — 小方块 - hhardyy.com&source=vue全家桶也就是1.脚手架、2.路由、3.状态管理、4.请求工具~。
" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://yoursite.com/2018/06/10/Vue全家桶/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Vue全家桶》 — 小方块 - hhardyy.com&url=http://yoursite.com/2018/06/10/Vue全家桶/&via=http://yoursite.com" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://yoursite.com/2018/06/10/Vue全家桶/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>








<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '(•‾̑⌣‾̑•)✧˖°回来看我';
            clearTimeout(titleTime);
        } else {
            document.title = '(゜-゜)つロ欢迎回来';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
